package demo.IOStream;

import java.io.*;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/15-08-15-19:59
 * @Description：demo.IOStream
 */
/*
编程实现：反转一个文本文件

例如，第一行和倒数第一行的数据交换

例如，第二行和倒数第二行的数据交换
 */
public class revanseFile {
    public static void main(String[] args) {
        BufferedReader reader = null;
        BufferedWriter writer = null;
        int lineLength = 0;
        try {
            reader = new BufferedReader(new FileReader(new File("cal.txt")));
            String s[] = new String[100];
            while((s[lineLength] = reader.readLine()) != null){
                System.out.println(s[lineLength]);
                lineLength++;
            }
            writer = new BufferedWriter(new FileWriter(new File("cal.txt")));
            for (int i = lineLength-1; i >= 0; i--) {
                writer.write(s[i]);
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(reader != null){
                    reader.close();
                }
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
